package com.myres.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Set;
import java.util.TreeSet;


public class OrderItem extends BasicObject implements Comparable<OrderItem> {
	public static final long serialVersionUID=983134314234l;
	
	private int oid;
	private int pid;//seems useless so as above.//comment by hikui
	private String name;
	private Order order;
	private Product product;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	private int amount;
	private float price;
	private byte commented;
	private byte saved;
	public byte getSaved() {
		return saved;
	}
	public void setSaved(byte saved) {
		this.saved = saved;
	}
	
  public int getOid() {
		return oid;
	}
	public void setOid(int oid) {
		this.oid = oid;
	}
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
		this.oid = order.getId();
	}
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
		this.pid = product.getId();
	}
	public int getAmount() {
		return amount;
	}
	public void setAmount(int amount) {
		this.amount = amount;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public byte getCommented() {
		return commented;
	}
	public void setCommented(byte commented) {
		this.commented = commented;
	}
	@Override
	public boolean equals(Object o){
		if(this==o)return true;
		if(!(o instanceof OrderItem))return false;
		else if(this.getId()==((OrderItem)o).getId()) return true;
		//else if(((OrderItem)o).getPid()==this.getPid()){
		//	return true;
		else{
			return false;
		}
	}
	public static Set<OrderItem> populate(ResultSet rs) throws SQLException{
		Set<OrderItem> i=new TreeSet<OrderItem>();
		while(rs.next()){
			OrderItem it=new OrderItem();
			it.setId(rs.getInt(1));
			it.setNO(rs.getString(2));
			it.setOid(rs.getInt(3));
			it.setPid(rs.getInt(4));
			it.setAmount(rs.getInt(5));
			it.setPrice(rs.getFloat(6));
			it.setCommented(rs.getByte(7));
			it.setSaved(rs.getByte(8));
			it.setName(rs.getString(9));
			i.add(it);
		}
		return i;
	}
public int compareTo(OrderItem item1){
		
		if(item1.getPid()>this.getPid()) return -1;
		else if(item1.getPid()==this.getPid()) return 0;
		else return 1;
}
}
